package com.haian.slidealbum.model.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.haian.slidealbum.R;
import com.haian.slidealbum.common.TouchImageView;
import com.haian.slidealbum.model.object.ObjectImage;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;


public class AdapterFullScreenImage extends PagerAdapter {
 
    private Activity _activity;
    ArrayList<ObjectImage> arrayImage;
    private LayoutInflater inflater;
	ImageLoader imageLoader;
	private DisplayImageOptions options;
    // constructor
    @SuppressWarnings("deprecation")
	public AdapterFullScreenImage(Activity activity,
            ArrayList<ObjectImage> imagePaths) {
        this._activity = activity;
        this.arrayImage = imagePaths;
        
		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(_activity).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher)
		.cacheInMemory()
		.cacheOnDisc()
		.resetViewBeforeLoading(true) // default
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
		.bitmapConfig(Bitmap.Config.RGB_565) // default
		.displayer(new FadeInBitmapDisplayer(300)) // default
		.handler(new Handler()) // default
		.build();
    }
 
    @Override
    public int getCount() {
        return this.arrayImage.size();
    }
 
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }
     
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    	TouchImageView imgDisplay;
        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.item_fullscreen_image, container,
                false);
  
        ObjectImage mObjectImage = arrayImage.get(position);
        imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgView);
        imageLoader.displayImage("file://"+mObjectImage.getUrlImage(), imgDisplay, options);
        ((ViewPager) container).addView(viewLayout);
        return viewLayout;
    }
     
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);
    }
}